#include "InputGLFW.h"
POCELAIN_NAMESPACE_BEGIN
Vector2 InputGLFW::smScrollDelta;
Vector2 InputGLFW::smWindowSize;
int InputGLFW::smMapKeyCode2GLFWKeyNum[int(EKeyCode::KeyCodeEnd)];
int InputGLFW::smMapGLFWKeyNum2KeyCode[GLFW_KEY_LAST+1];
int InputGLFW::smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_LAST+1];
bool InputGLFW::smHasInitKeyCodeMap = false;
IInput::EKeyState InputGLFW::smButtonStateCache[int(EKeyCode::KeyCodeEnd)];

void InputGLFW::initKeyCodeMap()
{
    // key code to glfw key
    smMapKeyCode2GLFWKeyNum[EKeyCode::None] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Backspace] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Delete] = GLFW_KEY_DELETE;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Tab] = GLFW_KEY_TAB;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Clear] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Return] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Pause] = GLFW_KEY_PAUSE;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Escape] = GLFW_KEY_ESCAPE;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Space] = GLFW_KEY_SPACE;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad0] = GLFW_KEY_KP_0;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad1] = GLFW_KEY_KP_1;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad2] = GLFW_KEY_KP_2;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad3] = GLFW_KEY_KP_3;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad4] = GLFW_KEY_KP_4;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad5] = GLFW_KEY_KP_5;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad6] = GLFW_KEY_KP_6;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad7] = GLFW_KEY_KP_7;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad8] = GLFW_KEY_KP_8;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Keypad9] = GLFW_KEY_KP_9;
    smMapKeyCode2GLFWKeyNum[EKeyCode::KeypadPeriod] = GLFW_KEY_KP_DECIMAL;
    smMapKeyCode2GLFWKeyNum[EKeyCode::KeypadDivide] = GLFW_KEY_KP_DIVIDE;
    smMapKeyCode2GLFWKeyNum[EKeyCode::KeypadMultiply] = GLFW_KEY_KP_MULTIPLY;
    smMapKeyCode2GLFWKeyNum[EKeyCode::KeypadMinus] = GLFW_KEY_KP_SUBTRACT;
    smMapKeyCode2GLFWKeyNum[EKeyCode::KeypadPlus] = GLFW_KEY_KP_ADD;
    smMapKeyCode2GLFWKeyNum[EKeyCode::KeypadEnter] = GLFW_KEY_KP_ENTER;
    smMapKeyCode2GLFWKeyNum[EKeyCode::KeypadEquals] = GLFW_KEY_KP_EQUAL;
    smMapKeyCode2GLFWKeyNum[EKeyCode::UpArrow] = GLFW_KEY_UP;
    smMapKeyCode2GLFWKeyNum[EKeyCode::DownArrow] = GLFW_KEY_DOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightArrow] = GLFW_KEY_RIGHT;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftArrow] = GLFW_KEY_LEFT;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Insert] = GLFW_KEY_INSERT;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Home] = GLFW_KEY_HOME;
    smMapKeyCode2GLFWKeyNum[EKeyCode::End] = GLFW_KEY_END;
    smMapKeyCode2GLFWKeyNum[EKeyCode::PageUp] = GLFW_KEY_PAGE_UP;
    smMapKeyCode2GLFWKeyNum[EKeyCode::PageDown] = GLFW_KEY_PAGE_DOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F1] = GLFW_KEY_F1;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F2] = GLFW_KEY_F2;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F3] = GLFW_KEY_F3;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F4] = GLFW_KEY_F4;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F5] = GLFW_KEY_F5;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F6] = GLFW_KEY_F6;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F7] = GLFW_KEY_F7;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F8] = GLFW_KEY_F8;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F9] = GLFW_KEY_F9;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F10] = GLFW_KEY_F10;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F11] = GLFW_KEY_F11;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F12] = GLFW_KEY_F12;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F13] = GLFW_KEY_F13;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F14] = GLFW_KEY_F14;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F15] = GLFW_KEY_F15;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F16] = GLFW_KEY_F16;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F17] = GLFW_KEY_F17;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F18] = GLFW_KEY_F18;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F19] = GLFW_KEY_F19;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F20] = GLFW_KEY_F20;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F21] = GLFW_KEY_F21;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F22] = GLFW_KEY_F22;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F23] = GLFW_KEY_F23;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F24] = GLFW_KEY_F24;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F25] = GLFW_KEY_F25;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha0] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha1] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha2] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha3] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha4] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha5] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha6] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha7] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha8] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Alpha9] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Exclaim] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::DoubleQuote] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Hash] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Dollar] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Percent] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Ampersand] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Quote] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftParen] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightParen] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Asterisk] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Plus] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Comma] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Minus] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Period] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Slash] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Colon] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Semicolon] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Less] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Equals] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Greater] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Question] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::At] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftBracket] = GLFW_KEY_LEFT_BRACKET;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Backslash] = GLFW_KEY_BACKSLASH;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightBracket] = GLFW_KEY_RIGHT_BRACKET;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Caret] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Underscore] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::BackQuote] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::A] = GLFW_KEY_A;
    smMapKeyCode2GLFWKeyNum[EKeyCode::B] = GLFW_KEY_B;
    smMapKeyCode2GLFWKeyNum[EKeyCode::C] = GLFW_KEY_C;
    smMapKeyCode2GLFWKeyNum[EKeyCode::D] = GLFW_KEY_D;
    smMapKeyCode2GLFWKeyNum[EKeyCode::E] = GLFW_KEY_E;
    smMapKeyCode2GLFWKeyNum[EKeyCode::F] = GLFW_KEY_F;
    smMapKeyCode2GLFWKeyNum[EKeyCode::G] = GLFW_KEY_G;
    smMapKeyCode2GLFWKeyNum[EKeyCode::H] = GLFW_KEY_H;
    smMapKeyCode2GLFWKeyNum[EKeyCode::I] = GLFW_KEY_I;
    smMapKeyCode2GLFWKeyNum[EKeyCode::J] = GLFW_KEY_J;
    smMapKeyCode2GLFWKeyNum[EKeyCode::K] = GLFW_KEY_K;
    smMapKeyCode2GLFWKeyNum[EKeyCode::L] = GLFW_KEY_L;
    smMapKeyCode2GLFWKeyNum[EKeyCode::M] = GLFW_KEY_M;
    smMapKeyCode2GLFWKeyNum[EKeyCode::N] = GLFW_KEY_N;
    smMapKeyCode2GLFWKeyNum[EKeyCode::O] = GLFW_KEY_O;
    smMapKeyCode2GLFWKeyNum[EKeyCode::P] = GLFW_KEY_P;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Q] = GLFW_KEY_Q;
    smMapKeyCode2GLFWKeyNum[EKeyCode::R] = GLFW_KEY_R;
    smMapKeyCode2GLFWKeyNum[EKeyCode::S] = GLFW_KEY_S;
    smMapKeyCode2GLFWKeyNum[EKeyCode::T] = GLFW_KEY_T;
    smMapKeyCode2GLFWKeyNum[EKeyCode::U] = GLFW_KEY_U;
    smMapKeyCode2GLFWKeyNum[EKeyCode::V] = GLFW_KEY_V;
    smMapKeyCode2GLFWKeyNum[EKeyCode::W] = GLFW_KEY_W;
    smMapKeyCode2GLFWKeyNum[EKeyCode::X] = GLFW_KEY_X;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Y] = GLFW_KEY_Y;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Z] = GLFW_KEY_Z;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftCurlyBracket] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Pipe] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightCurlyBracket] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Tilde] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Numlock] = GLFW_KEY_NUM_LOCK;
    smMapKeyCode2GLFWKeyNum[EKeyCode::CapsLock] = GLFW_KEY_CAPS_LOCK;
    smMapKeyCode2GLFWKeyNum[EKeyCode::ScrollLock] = GLFW_KEY_SCROLL_LOCK;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightShift] = GLFW_KEY_RIGHT_SHIFT;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftShift] = GLFW_KEY_LEFT_SHIFT;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightControl] = GLFW_KEY_RIGHT_CONTROL;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftControl] = GLFW_KEY_LEFT_CONTROL;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightAlt] = GLFW_KEY_RIGHT_ALT;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftAlt] = GLFW_KEY_LEFT_ALT;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftCommand] = GLFW_KEY_LEFT_SUPER;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftApple] = GLFW_KEY_LEFT_SUPER;
    smMapKeyCode2GLFWKeyNum[EKeyCode::LeftWindows] = GLFW_KEY_LEFT_SUPER;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightCommand] = GLFW_KEY_RIGHT_SUPER;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightApple] = GLFW_KEY_RIGHT_SUPER;
    smMapKeyCode2GLFWKeyNum[EKeyCode::RightWindows] = GLFW_KEY_RIGHT_SUPER;
    smMapKeyCode2GLFWKeyNum[EKeyCode::AltGr] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Help] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Print] = GLFW_KEY_PRINT_SCREEN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::SysReq] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Break] = GLFW_KEY_UNKNOWN;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Menu] = GLFW_KEY_MENU;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Mouse0] = GLFW_MOUSE_BUTTON_1;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Mouse1] = GLFW_MOUSE_BUTTON_2;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Mouse2] = GLFW_MOUSE_BUTTON_3;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Mouse3] = GLFW_MOUSE_BUTTON_4;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Mouse4] = GLFW_MOUSE_BUTTON_5;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Mouse5] = GLFW_MOUSE_BUTTON_6;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Mouse6] = GLFW_MOUSE_BUTTON_7;
    smMapKeyCode2GLFWKeyNum[EKeyCode::Mouse7] = GLFW_MOUSE_BUTTON_7;

    // glfw key to key code
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_SPACE] = EKeyCode::Space;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_APOSTROPHE] = EKeyCode::None;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_COMMA] = EKeyCode::Comma;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_MINUS] = EKeyCode::Minus;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_PERIOD] = EKeyCode::Period;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_SLASH] = EKeyCode::Slash;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_0] = EKeyCode::Alpha0;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_1] = EKeyCode::Alpha1;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_2] = EKeyCode::Alpha2;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_3] = EKeyCode::Alpha3;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_4] = EKeyCode::Alpha4;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_5] = EKeyCode::Alpha5;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_6] = EKeyCode::Alpha6;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_7] = EKeyCode::Alpha7;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_8] = EKeyCode::Alpha8;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_9] = EKeyCode::Alpha9;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_SEMICOLON] = EKeyCode::Semicolon;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_EQUAL] = EKeyCode::Equals;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_A] = EKeyCode::A;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_B] = EKeyCode::B;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_C] = EKeyCode::C;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_D] = EKeyCode::D;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_E] = EKeyCode::E;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F] = EKeyCode::F;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_G] = EKeyCode::G;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_H] = EKeyCode::H;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_I] = EKeyCode::I;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_J] = EKeyCode::J;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_K] = EKeyCode::K;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_L] = EKeyCode::L;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_M] = EKeyCode::M;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_N] = EKeyCode::N;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_O] = EKeyCode::O;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_P] = EKeyCode::P;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_Q] = EKeyCode::Q;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_R] = EKeyCode::R;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_S] = EKeyCode::S;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_T] = EKeyCode::T;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_U] = EKeyCode::U;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_V] = EKeyCode::V;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_W] = EKeyCode::W;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_X] = EKeyCode::X;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_Y] = EKeyCode::Y;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_Z] = EKeyCode::Z;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_LEFT_BRACKET] = EKeyCode::LeftBracket;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_BACKSLASH] = EKeyCode::Backslash;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_RIGHT_BRACKET] = EKeyCode::RightBracket;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_GRAVE_ACCENT] = EKeyCode::Tilde;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_WORLD_1] = EKeyCode::None;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_WORLD_2] = EKeyCode::None;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_ESCAPE] = EKeyCode::Escape;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_ENTER] = EKeyCode::Return;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_TAB] = EKeyCode::Tab;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_BACKSPACE] = EKeyCode::Backspace;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_INSERT] = EKeyCode::Insert;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_DELETE] = EKeyCode::Delete;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_RIGHT] = EKeyCode::RightArrow;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_LEFT] = EKeyCode::LeftArrow;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_DOWN] = EKeyCode::DownArrow;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_UP] = EKeyCode::UpArrow;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_PAGE_UP] = EKeyCode::PageUp;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_PAGE_DOWN] = EKeyCode::PageDown;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_HOME] = EKeyCode::Home;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_END] = EKeyCode::End;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_CAPS_LOCK] = EKeyCode::CapsLock;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_SCROLL_LOCK] = EKeyCode::ScrollLock;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_NUM_LOCK] = EKeyCode::Numlock;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_PRINT_SCREEN] = EKeyCode::Print;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_PAUSE] = EKeyCode::Pause;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F1] = EKeyCode::F1;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F2] = EKeyCode::F2;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F3] = EKeyCode::F3;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F4] = EKeyCode::F4;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F5] = EKeyCode::F5;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F6] = EKeyCode::F6;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F7] = EKeyCode::F7;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F8] = EKeyCode::F8;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F9] = EKeyCode::F9;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F10] = EKeyCode::F10;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F11] = EKeyCode::F11;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F12] = EKeyCode::F12;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F13] = EKeyCode::F13;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F14] = EKeyCode::F14;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F15] = EKeyCode::F15;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F16] = EKeyCode::F16;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F17] = EKeyCode::F17;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F18] = EKeyCode::F18;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F19] = EKeyCode::F19;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F20] = EKeyCode::F20;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F21] = EKeyCode::F21;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F22] = EKeyCode::F22;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F23] = EKeyCode::F23;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F24] = EKeyCode::F24;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_F25] = EKeyCode::F25;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_0] = EKeyCode::Keypad0;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_1] = EKeyCode::Keypad1;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_2] = EKeyCode::Keypad2;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_3] = EKeyCode::Keypad3;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_4] = EKeyCode::Keypad4;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_5] = EKeyCode::Keypad5;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_6] = EKeyCode::Keypad6;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_7] = EKeyCode::Keypad7;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_8] = EKeyCode::Keypad8;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_9] = EKeyCode::Keypad9;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_DECIMAL] = EKeyCode::KeypadPeriod;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_DIVIDE] = EKeyCode::KeypadDivide;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_MULTIPLY] = EKeyCode::KeypadMultiply;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_SUBTRACT] = EKeyCode::KeypadMinus;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_ADD] = EKeyCode::KeypadPlus;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_ENTER] = EKeyCode::KeypadEnter;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_KP_EQUAL] = EKeyCode::KeypadEquals;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_LEFT_SHIFT] = EKeyCode::LeftShift;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_LEFT_CONTROL] = EKeyCode::LeftControl;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_LEFT_ALT] = EKeyCode::LeftAlt;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_LEFT_SUPER] = EKeyCode::LeftCommand;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_RIGHT_SHIFT] = EKeyCode::RightShift;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_RIGHT_CONTROL] = EKeyCode::RightControl;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_RIGHT_ALT] = EKeyCode::RightAlt;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_RIGHT_SUPER] = EKeyCode::RightCommand;
    smMapGLFWKeyNum2KeyCode[GLFW_KEY_MENU] = EKeyCode::Menu;

    smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_1] = EKeyCode::Mouse0;
    smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_2] = EKeyCode::Mouse1;
    smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_3] = EKeyCode::Mouse2;
    smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_4] = EKeyCode::Mouse3;
    smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_5] = EKeyCode::Mouse4;
    smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_6] = EKeyCode::Mouse5;
    smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_7] = EKeyCode::Mouse6;
    smMapGLFWMouseKeyNum2KeyCode[GLFW_MOUSE_BUTTON_8] = EKeyCode::Mouse7;

    for(auto &i : smButtonStateCache)
    {
        i = EKeyState::Up;
    }
    smHasInitKeyCodeMap = true;
}
int InputGLFW::keyCodeTranform(EKeyCode key)
{
    if(!smHasInitKeyCodeMap)
        initKeyCodeMap();
    return smMapKeyCode2GLFWKeyNum[int(key)];
}

InputGLFW::InputGLFW(GLFWwindow *window)
{
    POCELAIN_ASSERT(window!=nullptr);
    mWindow = window;
    // when call glfwPollEvents , these callback may triggered;
    glfwSetKeyCallback(window, [](GLFWwindow* , int key, int scancode, int action, int mods) {
        // only focus on key pressed and key released;
        if(action == GLFW_REPEAT)
            return;
        auto keyCode = smMapGLFWKeyNum2KeyCode[key];
        if(keyCode==EKeyCode::None)
        {
            assert(false);
        }
        else
        {
            auto &state = smButtonStateCache[keyCode];
            if(action == GLFW_PRESS)
            {
                state = EKeyState::Pressed;
            }
            else
            {
                state = EKeyState::Released;
            }
        }
    });
    glfwSetMouseButtonCallback(window, [](GLFWwindow* , int button, int action, int mods) {
        // only focus on key pressed and key released;
        if(action == GLFW_REPEAT)
            return;
        auto keyCode = smMapGLFWMouseKeyNum2KeyCode[button];
        if(keyCode==EKeyCode::None)
        {
            assert(false);
        }
        else
        {
            auto &state = smButtonStateCache[keyCode];
            if(action == GLFW_PRESS)
            {
                state = EKeyState::Pressed;
            }
            else
            {
                state = EKeyState::Released;
            }
        }
    });
    glfwSetScrollCallback(window, [](GLFWwindow* , double xoffset, double yoffset) {
        InputGLFW::smScrollDelta.set(xoffset, yoffset);
    });
    glfwSetWindowSizeCallback(mWindow,[](GLFWwindow* , int width, int height){
        InputGLFW::smWindowSize.set(width,height) ;
    });
    {
        int width , height;
        glfwGetWindowSize(mWindow,&width, &height);
        smWindowSize.set(width,height);
    }
}

void InputGLFW::refreshKeyState()
{
    for(auto &i : smButtonStateCache)
    {
        if(i==EKeyState::Pressed)
        {
            i = EKeyState::Down;
        }
        else if(i == EKeyState::Released)
        {
            i = EKeyState::Up;
        }
    }
}

void InputGLFW::update()
{
    refreshKeyState();
    smScrollDelta.setAll(0.0);
    glfwPollEvents();
    double x, y;
    mLastCursor = mCursor;
    glfwGetCursorPos(mWindow, &x, &y);
    mCursor.x = float(x);
    mCursor.y = float(y);
}
bool InputGLFW::anyKey()
{
    //TODO
    return false;
}
bool InputGLFW::getKey(EKeyCode code)
{
    int glfwKeyCode = keyCodeTranform(code);
    if(glfwKeyCode== GLFW_KEY_UNKNOWN)
    {
        POCELAIN_LOG_WARN("Unknown GLFW key of key code %d",code);
        return false;
    }
    auto state = smButtonStateCache[code];
    return state == EKeyState::Down || state == EKeyState::Pressed;
}
bool InputGLFW::getKeyDown(EKeyCode code)
{
    int glfwKeyCode = keyCodeTranform(code);
    if(glfwKeyCode== GLFW_KEY_UNKNOWN)
    {
        POCELAIN_LOG_WARN("Unknown GLFW key of key code %d",code);
        return false;
    }

    auto state = smButtonStateCache[code];
    return state == EKeyState::Pressed;
}
bool InputGLFW::getKeyUp(EKeyCode code)
{
    int glfwKeyCode = keyCodeTranform(code);
    if(glfwKeyCode== GLFW_KEY_UNKNOWN)
    {
        POCELAIN_LOG_WARN("Unknown GLFW key of key code %d",code);
        return false;
    }
    return smButtonStateCache[code] == EKeyState::Released;
}
bool InputGLFW::getButton(std::string button)
{
    return false;
}
Real InputGLFW::getAxis(std::string axis)
{
    return Real(0);
}
Vector2 InputGLFW::getMousePosition()
{
    return mCursor;
}
Vector2 InputGLFW::getMousePositionDelta()
{
    return mCursor-mLastCursor;
}
Vector2 InputGLFW::getMouseScrollDelta()
{
    auto ret = smScrollDelta;
    return ret;
}
Vector2 InputGLFW::getWindowSize()
{
    return smWindowSize;
}

void InputGLFW::setCursorState(ECursorState state)
{
    if(getCursorState()!=state)
    {
        IInput::setCursorState(state);
        switch(state)
        {
        case Normal:
        {
            glfwSetInputMode(mWindow,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
            break;
        }
        case Hidden:
        {
            glfwSetInputMode(mWindow,GLFW_CURSOR,GLFW_CURSOR_HIDDEN);
            break;
        }
        case Disable:
        {
            glfwSetInputMode(mWindow,GLFW_CURSOR,GLFW_CURSOR_DISABLED);
            break;
        }
        default:
            POCELAIN_ASSERT(false);
            POCELAIN_LOG_ERROR("Unknown Cursor state");
            break;
        }
    }
}
POCELAIN_NAMESPACE_END
